package com.theobserver.theobserver;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.GetDataCallback;

public class UserCenterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usercenter);
        // final int SHOW_UserNameModifyActivity=1;
        //AVUser user = new AVUser();
        //获取当前用户
        //AVUser.getCurrentUser();
        //setResult(Code.STATEHOLD);
        final TextView tt=(TextView) findViewById(R.id.textView_nicknametext);
        AVUser t_user = AVUser.getCurrentUser();
        tt.setText(t_user.getString("user_name"));

        //对用户名和邮箱进行获取
        String user1 = AVUser.getCurrentUser().getUsername();//用户名
        String Email = AVUser.getCurrentUser().getEmail();//邮箱
        TextView tt1=(TextView) findViewById(R.id.textView_displayID);
        tt1.setText(Email);

        //对头像获取（判断是否显示默认头像）

        final ImageView iv=(ImageView)findViewById(R.id.imageView_head);
        AVFile t_head = t_user.getAVFile("user_head");
        if(t_head != null) {
            t_head.getDataInBackground(new GetDataCallback() {
                @Override
                public void done(byte[] bytes, AVException e) {
                    if (e == null) {
                        iv.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                    }
                }
            });
        }

        //向修改用户名界面跳转
        TextView tv1 = (TextView) findViewById(R.id.textView_alternickname);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(UserCenterActivity.this, UserNameModifyActivity.class));
                startActivityForResult(new Intent(UserCenterActivity.this, UserNameModifyActivity.class), Code.USERNAMEMODIFYACTIVITY);
            }
        });

        //向修改密码界面跳转
        TextView tv2 = (TextView) findViewById(R.id.textView_alpassword);
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(UserCenterActivity.this, UserPasswdModifyActivity.class), Code.USERPASSWDMODIFYACTIVITY);
            }
        });

        //对“退出登录”按钮设置监听
        Button bt = (Button) findViewById(R.id.button_signout);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //AVUser.logOut();// 清除缓存用户对象
                // AVUser currentUser = AVUser.getCurrentUser();// 现在的 currentUser 是 null 了
                AVUser.getCurrentUser().logOut();
                UserCenterActivity.this.finish();
            }
        });
        //对“已发布新闻”设置监听，需要判断用户是否是作者
        TextView et2=(TextView)findViewById(R.id.textView_news) ;
        et2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(AVUser.getCurrentUser().getInt("user_isreporter") != 1)
                {
                    Toast.makeText(UserCenterActivity.this, "您不是作者!", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    startActivity(new Intent(UserCenterActivity.this, NewsSendedActivity.class));
                }

            }
        });


        //修改用户头像
        TextView change_head = (TextView) findViewById(R.id.textView_alterhead);
        change_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(UserCenterActivity.this, UserHeadModifyActivity.class), Code.USERHEADMODIFYACTIVITY);
            }
        });

        //对返回按钮设置监听，有点击时，退出用户中心界面
        TextView et = (TextView) findViewById(R.id.textView_return);
        et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserCenterActivity.this.finish();
            }
        });

    }

    //返回码和请求码的设置与处理
        public void onActivityResult(int requestCode,int ResuleCode,Intent data)
        {
            super.onActivityResult(requestCode,requestCode,data);
            final TextView tt=(TextView) findViewById(R.id.textView_nicknametext);


            switch (requestCode){
                case(Code.USERNAMEMODIFYACTIVITY):
                    if(ResuleCode== Code.STATECHANGE)
                    {
                        AVUser.getCurrentUser().fetchInBackground(new GetCallback<AVObject>() {
                            @Override
                            public void done(AVObject avObject, AVException e) {
                                if(e == null)
                                {
                                    tt.setText(AVUser.getCurrentUser().getString("user_name"));
                                }
                            }
                        });
                    }

                    break;
                case Code.USERPASSWDMODIFYACTIVITY:
                    if(AVUser.getCurrentUser() == null)
                    {
                        setResult(Code.STATECHANGE);
                        finish();
                    }
                    break;
                //从修改头像页面返回
                case Code.USERHEADMODIFYACTIVITY:
                    AVFile t_user_head = AVUser.getCurrentUser().getAVFile("user_head");

                    if(t_user_head != null)
                    {
                        t_user_head.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] bytes, AVException e) {
                                if(bytes != null)
                                {
                                    ImageView iv=(ImageView)findViewById(R.id.imageView_head);
                                    iv.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                                }
                            }
                        });
                    }

                    break;
            }
}


}
